#include <iostream>
#include "module.hxx"

#ifdef __cplusplus
extern "C" {
#endif
#include <string.h>
#ifdef __cplusplus
}
#endif

int demo_arg = 0;
struct moduleRuntimeArg runtimeArg = { 0 };

// 模块执行动作前的通知
void demo_notify(int action, void *arg)
{
    struct moduleActionArg *actArg = (struct moduleActionArg*) arg;
    struct moduleRuntimeArg * pRuntimeArg = nullptr;
    std::cout << "demo_notify : " << std::endl;
    switch (action) {
    case ModuleActionSetRuntimeArg :
        pRuntimeArg = (struct moduleRuntimeArg *)actArg->runtimeArg;
        std::cout << "pRuntimeArg : " << pRuntimeArg << std::endl;
        if(pRuntimeArg == nullptr)
        {
            memset(&runtimeArg, 0, sizeof(runtimeArg));
        }else
        {
            std::cout << "ModuleActionSetRuntimeArg Size: " << pRuntimeArg->size << std::endl;
            std::cout << "ModuleActionSetRuntimeArg Data: " << pRuntimeArg->data << std::endl;
            memcpy(&runtimeArg, pRuntimeArg, sizeof(runtimeArg));
        }
        break;
#if 0
    // 基本上，下列模块的事件是不需要被关心的
    case ModuleActionPreinit:
        break;
    case ModuleActionInit:
        break;
    case ModuleActionRun:
        break;
    case ModuleActionQuit:
        break;
    case ModuleActionGetPendingData:
        break;
#endif
    /* 设置 模块执行结果 */
    // 如果一个模块需要手动设置执行结果，那通常也意味着此后不再需要它继续执行了。
    case ModuleActionSetRunResult:
        std::cout << "ModuleActionSetRunResult, maybe we can stop runing" << std::endl;
        break;
	default: break;
    }
    return;
}

void demo_preInit(void*arg)
{
    int i = *(int*)arg;

    demo_arg++;

    std::cout << "demo_preInit : " << i << std::endl;
    if(arg)
        i = *(int*)arg;
    //std::cout << "demo_preInit : " << i << std::endl;
    return;
}

bool demo_init(void*arg)
{
    int i = *(int*)arg;

    demo_arg++;
    std::cout << "demo_init : " << i << std::endl;
    return TestResultAsPass;
}

bool demo_run(void*arg)
{
    int i = *(int*)arg;

    demo_arg++;
    std::cout << "demo_run : " << i << std::endl;
    return TestResultAsPass;
}

void demo_quit(void*arg)
{
    int i = *(int*)arg;

    demo_arg++;
    std::cout << "demo_quit : " << i << std::endl;
    return;
}

#if 0
// 全局初始化类无法实现，只能通过这样子的形式包含，未免过于丑陋
// 而且不方便添加
class TestModule *getDemoModule(void)
{
    static class TestModule demo("demo",
                           TestResultAsPass,
                           ModuleEnable,
                           ModuleAutoEnable);
    demo.setRun(demo_run);
    //demo.run(nullptr);
    return &demo;
}
#endif
